package com.marvels.approve.consum.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.marvels.approve.consum.ApproveConsumerProductApi;
import com.marvels.approve.loan.dto.CascadeChannelDto;
import com.marvels.approve.loan.dto.McptChannelDto;
import com.marvels.approve.loan.dto.McptConsultingFeeDTO;
import com.marvels.approve.loan.dto.McptProductType;
import com.marvels.approve.loan.dto.McptProductTypeDto;
import com.marvels.approve.process.dto.McptCustomerTypeParamDTO;
import com.marvels.approve.process.dto.McptManagementFeeDTO;
import com.marvels.approve.process.dto.McptProduct;
import com.marvels.approve.process.dto.McptProductFundRelationDto;
import com.marvels.approve.process.dto.McptProductMappedDto;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.product.param.api.ProductChannelApi;
import com.marvels.product.param.api.ProductTypeApi;
import com.marvels.product.param.service.ConsultingFeeService;
import com.marvels.product.param.service.McptCustomerTypeParamApi;
import com.marvels.product.param.service.MngFeeService;
import com.marvels.product.param.service.ProductItemService;
import com.marvels.product.param.service.ProductService;
import com.marvels.product.productmanage.service.ProductFundRelationApi;
import com.marvels.product.productmanage.service.ProductTypeService;

import cn.hutool.core.bean.BeanUtil;

@Component
public class ApproveConsumProductImpl implements ApproveConsumerProductApi{

	@Autowired
	private ProductChannelApi productChannelApi;
	
	@Autowired
	private ProductTypeApi productTypeApi;
	
	@Autowired
	private ProductItemService productItemService;
	
	@Autowired
	private ProductTypeService productTypeService;
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ConsultingFeeService consultingFeeService;
	
	@Autowired
	private ProductFundRelationApi relationApi;
	
	@Autowired
	private McptCustomerTypeParamApi typeParamApi;
	
	@Autowired
	private MngFeeService mngFeeService;
	
	@Override
	public CascadeChannelDto mcptChannelInfo(McptChannelDto mcptChannelDto) {
		try {
			com.marvels.product.productmanage.dto.McptChannelDto param = new com.marvels.product.productmanage.dto.McptChannelDto();
			BeanUtil.copyProperties(mcptChannelDto, param);
			com.marvels.product.param.dto.CascadeChannelDto apiResult = productChannelApi.mcptChannelInfo(param);
			CascadeChannelDto result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), CascadeChannelDto.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.mcptChannelInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.mcptChannelInfo error", e);
			return null;
		}
	}

	@Override
	public List<McptProductType> mcptProductTypeInfo(McptProductType productType) {
		try {
			com.marvels.product.param.dto.McptProductType param = new com.marvels.product.param.dto.McptProductType();
			BeanUtil.copyProperties(productType, param);
			List<com.marvels.product.param.dto.McptProductType> apiResult = productTypeApi.mcptProductTypeInfo(param);
			List<McptProductType> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptProductType.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.mcptProductTypeInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.mcptProductTypeInfo error", e);
			return null;
		}
	}

	

	@Override
	public List<McptProductFundRelationDto> selectProductFundRelation(McptProductFundRelationDto fundRelationDto) {
		try {
			com.marvels.product.productmanage.dto.McptProductFundRelationDto param = new com.marvels.product.productmanage.dto.McptProductFundRelationDto();
			BeanUtil.copyProperties(fundRelationDto, param);
			List<com.marvels.product.productmanage.dto.McptProductFundRelationDto> apiResult = relationApi.selectProductFundRelation(param);
			List<McptProductFundRelationDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptProductFundRelationDto.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.selectProductFundRelation direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.selectProductFundRelation error", e);
			return null;
		}
	}

	@Override
	public McptCustomerTypeParamDTO selectTypeById(McptCustomerTypeParamDTO customerTypeParamDTO) {
		try {
			com.marvels.product.param.dto.McptCustomerTypeParamDTO apiResult=typeParamApi.selectTypeById(customerTypeParamDTO.getCustomerType());
			McptCustomerTypeParamDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptCustomerTypeParamDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.selectTypeById direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.selectTypeById error", e);
			return null;
		}
	}

	@Override
	public List<McptCustomerTypeParamDTO> selectAllType() {
		try {
			List<com.marvels.product.param.dto.McptCustomerTypeParamDTO> apiResult = typeParamApi.selectAllType();
			List<McptCustomerTypeParamDTO> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptCustomerTypeParamDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.selectAllType direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.selectAllType error", e);
			return null;
		}
	}


	@Override
	public McptManagementFeeDTO selectManagementFeeByProductInfo(McptProduct product) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(product, param);
			com.marvels.product.param.dto.McptManagementFeeDTO apiResult = mngFeeService.selectByProductInfo(param);
			McptManagementFeeDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptManagementFeeDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.selectManagementFeeByProductInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.selectManagementFeeByProductInfo error", e);
			return null;
		}
	}

	@Override
	public McptConsultingFeeDTO mcptConsultingFeeDTO(McptProduct mcptProduct) {
		try {
			com.marvels.product.param.dto.McptProduct param = new com.marvels.product.param.dto.McptProduct();
			BeanUtil.copyProperties(mcptProduct, param);
			com.marvels.product.param.dto.McptConsultingFeeDTO apiResult = consultingFeeService.selectByProductInfo(param);
			McptConsultingFeeDTO result = JSONObject.parseObject(JSONObject.toJSONString(apiResult), McptConsultingFeeDTO.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.mcptConsultingFeeDTO direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.mcptConsultingFeeDTO error", e);
			return null;
		}
	}


	@Override
	public List<McptChannelDto> mcptChannelAllInfo(McptChannelDto mcptChannelDto) {
		try {
			com.marvels.product.productmanage.dto.McptChannelDto param = new com.marvels.product.productmanage.dto.McptChannelDto();
			BeanUtil.copyProperties(mcptChannelDto, param);
			List<com.marvels.product.productmanage.dto.McptChannelDto> apiResult = productChannelApi.mcptChannelAllInfo(param);
			List<McptChannelDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptChannelDto.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.mcptChannelAllInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.mcptChannelAllInfo error", e);
			return null;
		}
	}

	@Override
	public List<McptProductMappedDto> selectProductMappedInfo(McptProductMappedDto product) {
		try {
			com.marvels.product.productmanage.dto.McptProductMappedDto param = new com.marvels.product.productmanage.dto.McptProductMappedDto();
			BeanUtil.copyProperties(product, param);
			List<com.marvels.product.productmanage.dto.McptProductMappedDto> apiResult = productItemService.selectProductMappedInfo(param);
			List<McptProductMappedDto> result = JSONObject.parseArray(JSONObject.toJSONString(apiResult), McptProductMappedDto.class);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.selectProductMappedInfo direct call success!");
			return result;
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.selectProductMappedInfo error", e);
			return null;
		}
	}

	@Override
	public MarvelsResultDTO searchProduct(McptProductTypeDto mcptProducTypeDto) {
		try {
			com.marvels.product.productmanage.dto.McptProductTypeDto param = new com.marvels.product.productmanage.dto.McptProductTypeDto();
			BeanUtil.copyProperties(mcptProducTypeDto, param);
			List<com.marvels.product.productmanage.dto.McptProductTypeDto> list = productTypeService.queryProduct(param);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.searchProduct direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.searchProduct error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),e.getMessage());
		}
	}

	@Override
	public MarvelsResultDTO searchByParams(McptProductMappedDto mapped) {
		try { 
			com.marvels.product.productmanage.dto.McptProductMappedDto param = new com.marvels.product.productmanage.dto.McptProductMappedDto();
			BeanUtil.copyProperties(mapped, param);
			List<com.marvels.product.param.dto.McptProduct> list = productService.searchAllByParams(param);
			MarvelsLogUtil.getInstance().info("ApproveConsumerProductApi.searchByParams direct call success!");
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(), MarvelsErrorCode.SUCCESS.getDesc(),list);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("ApproveConsumerProductApi.searchByParams error", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), MarvelsErrorCode.ERROR.getDesc(),e.getMessage());
		}
	}

}
